a = int(input())
c = a
l = [2, 3, 5, 7]
n = [1, 3, 7, 9]
b = [0]
if 1000<=c and c<10000:
    while c!=0:
        for i in l:
            if c%10 == i:
                b.append(c%10)
        c//=10
    c = a
    while c!=0:
        for i in n:
            if c%10 == i and c%100%3!=0 and c%100%11!=0 and c%100%7!=0 and c%100 != 1:
                b.append(c%100)
        c//=10
    c = a
    while c!=0:
        for i in n:
            if c%10 == i and c%1000%3!=0 and c%1000%11!=0 and c%1000%7!=0 and c%1000 != 1:
                b.append(c%1000)
        c//=10
    c = a
    for i in n:
        if c%10 == i and c%3!=0 and c%11!=0 and c%7!=0:
            b.append(c)
print(max(b))
